package com.tian.controller.management;


import com.tian.common.annotation.HasPermission;
import com.tian.common.annotation.LoginCheckAnnotation;
import com.tian.common.util.Result;
import com.tian.dto.management.req.UserInfoAddReqDto;
import com.tian.dto.management.req.UserInfoReqDto;
import com.tian.dto.management.resp.PageDto;
import com.tian.dto.management.resp.UserInfoRespDto;
import com.tian.entity.UserInfo;
import com.tian.service.impl.management.RolePermissionService;
import com.tian.service.management.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * {@code @description:} 用户信息控制层
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-07-28 12:05
 * {@code @version:} 1.0
 */
@RestController
@RequestMapping("/management/user/info")
@Api(value = "用户信息接口", tags = "用户信息")
public class UserInfoController {

    @Resource
    private UserInfoService userInfoService;
    @Resource
    private RolePermissionService rolePermissionService;

    @LoginCheckAnnotation
    @HasPermission("staff.management:query")
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询用户信息", notes = "根据id查询用户信息"
            , produces = "application/json", consumes = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Integer")
    })
    public Result<UserInfo> findById(@PathVariable("id") Integer id) {
        return Result.success(userInfoService.getById(id));
    }

    @LoginCheckAnnotation
    @HasPermission("staff.management:query")
    @GetMapping("/condition")
    @ApiOperation(value = "根据条件查询用户信息")
    public Result findByCondition(@Validated UserInfoReqDto userInfoReqDto) {
        return userInfoService.findByCondition(userInfoReqDto);
    }


    @PostMapping("/login")
    public Result<UserInfoRespDto>  login(@RequestBody UserInfoReqDto userInfoReqDto){
        rolePermissionService.initCache();
        return userInfoService.login(userInfoReqDto);
    }


    @LoginCheckAnnotation
    @HasPermission("staff.management:query")
    @GetMapping("/list")
    @ApiOperation(value = "根据条件查询用户信息")
    public Result<PageDto<List<UserInfoRespDto>>>  list(UserInfoReqDto userInfoReqDto) {
        return userInfoService.list(userInfoReqDto);
    }

    @LoginCheckAnnotation
    @HasPermission("staff.management:add")
    @PostMapping("/registry")
    public Result<Boolean> registry(@RequestBody @Validated UserInfoAddReqDto userInfoAddReqDto){
        return userInfoService.registry(userInfoAddReqDto);
    }
    @LoginCheckAnnotation
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody @Validated  UserInfoAddReqDto userInfoAddReqDto){
        return userInfoService.update(userInfoAddReqDto);
    }

    @LoginCheckAnnotation
    @HasPermission("staff.management:delete")
    @GetMapping("/delete/{id}")
    public Result<Boolean> delete(@RequestBody @PathVariable("id") Integer id){
        return userInfoService.delete(id);
    }
}
